package com.simple.util;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;

import com.simple.util.common.Constants;
import com.simple.util.object.JFValidationBean;


public class ErrorsUtil{
    /**
     * return java script of error
     * @param locale
     * @param errorList
     * @return
     */
    public static String getJavascriptErrorMessage(final String locale, final List<JFValidationBean> errorList){       
        //delete the reduplicate error
        final List<JFValidationBean> identityErrorList = deleteReduplicateError(errorList);
        
        final Map<String,String> tbResourcesMap = getResourceMap(locale);
        
        final StringBuffer sbMsg = new StringBuffer();
        final StringBuffer sbDiv = new StringBuffer();
        for(int i=0;i<identityErrorList.size();i++){
            final JFValidationBean jfValidationBean = (JFValidationBean)errorList.get(i);
            
            if(jfValidationBean.getValidationId().indexOf(JFValidationBean.VALIDATION_ID_NO_ID)==0){
                sbMsg.append(jfValidationBean.getValidationMsg());
                sbMsg.append("\\n");
            }else{
                String msg = tbResourcesMap.get(jfValidationBean.getValidationId());
                if(msg==null){
                    sbMsg.append(jfValidationBean.getValidationId());
                    sbMsg.append("\\n");
                }
                else{
                    if(!jfValidationBean.getParamList().isEmpty()){
                        msg = MessageFormat.format(msg, jfValidationBean.getParamList().toArray());
                    }
                    sbMsg.append(msg);
                    sbMsg.append("\\n");
                }
            }
        }
        
        String rtn = sbDiv.toString();
        if(sbMsg.length()>0){
            rtn = sbDiv.append("alert('"+sbMsg.toString()+"');").toString();
        }
        if(rtn.length()>0){
            final StringBuffer sb = new StringBuffer(200);
            rtn=sb.append("<SCRIPT language='javascript'>$(document).ready(function(){"+rtn+"});</SCRIPT>").toString();
        }
        return rtn;
    }
    
    
    /**
     * return error hashmap: "DIV" (div list), "MESSAGE" (message list)
     * @param locale
     * @param errorList
     * @return
     */
    public static List<String> getErrorList(final String locale, final List<JFValidationBean> errorList){       
        //delete the reduplicate error
        final List<JFValidationBean> identityErrorList = deleteReduplicateError(errorList);
        
        final Map<String,String> tbResourcesMap = getResourceMap(locale);
        
        final List<String> msgList = new ArrayList<String>();
        
        for(int i=0;i<identityErrorList.size();i++){
	        final JFValidationBean jfValidationBean = (JFValidationBean)errorList.get(i);
	
	        if(jfValidationBean.getValidationId().indexOf(JFValidationBean.VALIDATION_ID_NO_ID)==0){
	            msgList.add(jfValidationBean.getValidationMsg());
	        }else{
	            String msg = tbResourcesMap.get(jfValidationBean.getValidationId());
	            if(msg==null){
	                msgList.add(jfValidationBean.getValidationId());
	            }
	            else{
	                if(!jfValidationBean.getParamList().isEmpty()){
	                    msg = MessageFormat.format(msg, jfValidationBean.getParamList().toArray());
	                }
	                msgList.add(msg);
	            }
	        }
        }

        
        return msgList;
    }
    
    /**
     * return error message , all message separate with sep
     * @param locale
     * @param errorList
     * @param sep
     * @return
     */
    public static String getErrorMessage(final String locale, final List<JFValidationBean> errorList, final String sep){       
        //delete the reduplicate error
        final List<JFValidationBean> identityErrorList = deleteReduplicateError(errorList);
        
        final Map<String,String> tbResourcesMap = getResourceMap(locale);
        
        final StringBuffer sbMsg = new StringBuffer();
        
        for(int i=0;i<identityErrorList.size();i++){
            final JFValidationBean jfValidationBean = (JFValidationBean)errorList.get(i);

            if(jfValidationBean.getValidationId().indexOf(JFValidationBean.VALIDATION_ID_NO_ID)==0){
                sbMsg.append(jfValidationBean.getValidationMsg()).append(sep);
            }else{
                String msg = tbResourcesMap.get(jfValidationBean.getValidationId());
                if(msg==null){
                    sbMsg.append(jfValidationBean.getValidationId()).append(sep);
                }
                else{
                    if(!jfValidationBean.getParamList().isEmpty()){
                        msg = MessageFormat.format(msg, jfValidationBean.getParamList().toArray());
                    }
                    sbMsg.append(msg).append(sep);
                }
            }
        }
        
        
        return sbMsg.toString();
    }
    
    private static List<JFValidationBean> deleteReduplicateError(final List<JFValidationBean> errorList){
        final LinkedMap errorMap = new LinkedMap();
        final List<JFValidationBean> identityErrorList = new ArrayList<JFValidationBean>();
        final Iterator<JFValidationBean> iteError=errorList.iterator();
        while ( iteError.hasNext()){
            final JFValidationBean jfValidationBean = (JFValidationBean)iteError.next();
            final String key = jfValidationBean.getValidationId() + jfValidationBean.getValidationMsg() + jfValidationBean.getParamList().toString();
            if(!errorMap.containsKey(key)){
                errorMap.put(key,jfValidationBean);
                identityErrorList.add(jfValidationBean);
            }
        }
        return identityErrorList;
    }
    
    protected static Map<String,String> getResourceMap(final String locale){
        return TbResourceLoad.getInstance().getMap(new String[]{Constants.TBRESOURCES_PACKAGE_MESSAGE}, locale);
    }
}
